//
//  ProductsDetailsInsidePagesViewController.m
//  Jingjing
//
//  Created by user on 15/11/20.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "ProductsDetailsInsidePagesViewController.h"

#import "ApplyConfirmationViewController.h"
#import "DetailActivity.h"
#import "LoginViewController.h"

@interface ProductsDetailsInsidePagesViewController ()<UIWebViewDelegate>
{
    UIWebView *_webView;
}
@property (nonatomic,strong)UIButton    * aplyBtn;
@property(nonatomic,strong)  NSMutableArray *dataSourceArr;
@property (nonatomic, strong) UIScrollView *backgroundScrollView;
@property (strong, nonatomic)UIView *noDataView;
@property(nonatomic,strong)UIView *noNet;

@end
@implementation ProductsDetailsInsidePagesViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:self.navTitle];
    [self.view addSubview:topView];
    
    self.view.backgroundColor = [UIColor hexStringToColor:@"#f3f3f3"];
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 20.0f, 44.0f, 44.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
    
    _backgroundScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f)];
    [self.view addSubview:_backgroundScrollView];
    
    
    
    if ([self.valid isEqualToString:@"1"]) {
        self.aplyBtn.hidden = NO;
       
    }else{
        self.aplyBtn.hidden = YES;
       
    }
    
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
   
    
   
    
   
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(5.0f,  5.0f, SCREEN_WIDTH - 10.0f, SCREEN_HEIGHT - 5.0f-64.0f)];
    _webView.delegate = self;
    _webView.scrollView.showsHorizontalScrollIndicator = FALSE;
    _webView.scrollView.showsVerticalScrollIndicator = FALSE;
    _webView.scrollView.scrollEnabled = FALSE;
    _webView.hidden = FALSE;
    [_backgroundScrollView addSubview:_webView];
    [_backgroundScrollView addSubview:self.noDataView];
    [_backgroundScrollView addSubview:self.noNet];

   [_backgroundScrollView  addSubview:self.aplyBtn];
    

    
     [self httpRequest];
}


- (void)httpRequest
{
    _noDataView.hidden = TRUE;
    _noNet.hidden = TRUE;
    

    [[AnimationIndicator sharedIndicator]startAnimationWithLoadText:@"加载中..."];
    [AppGlobalClass PostDataWithNSstring:GETACTIVITIESDETAIL withDictionary:@{@"activitiesId":self.activitesTypeId} success:^(NSDictionary *dictionary) {
        
        
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            NSMutableArray *listArray = [[[dictionary objectForKey:@"responseData"]firstObject] objectForKey:@"rows"];
            
          
            if (listArray.count==0) {
                _noDataView.hidden = FALSE;
            }else{
                _webView.hidden = FALSE;
                DetailActivity *detailActivity = [[DetailActivity alloc]initWithDictionary:[listArray objectAtIndex:0]];
                [_dataSourceArr addObject:detailActivity];
                [_webView loadHTMLString:detailActivity.content baseURL:nil];
            }

            
            
            
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            
            ALERT_VIEW(str);
            _aplyBtn.hidden = TRUE;
            [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:YES];
        }
        
    } failure:^(NSError *error) {
        _noDataView.hidden = FALSE;
        _aplyBtn.hidden = TRUE;
        _webView.hidden = TRUE;
         [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:YES];
    } networkerror:^(NSString *networkerror) {
        _noNet.hidden = FALSE;
        _aplyBtn.hidden = TRUE;
        _webView.hidden = TRUE;

         [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:YES];
    }];

}
- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 没网提示
-(UIView *)noNet
{
    if (!_noNet) {
        _noNet = [[UIView alloc]initWithFrame:CGRectMake((_webView.frame.size.width-200)/2, (_webView.frame.size.height-200)/2, 200, 235)];
        _noNet.hidden = TRUE;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(45, 23, 110, 110);
        [button setImage:[UIImage imageNamed:@"img_nonetwork"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(noResultButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 - 44, 200, 44)];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:@"加载失败\n请检查手机是否联网"];
        
        [aString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],
                                 NSForegroundColorAttributeName:[UIColor colorWithWhite:0.098 alpha:1.000],
                                 } range:NSMakeRange(0, 5)];
        
        [aString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],
                                 NSForegroundColorAttributeName:[UIColor colorWithWhite:0.702 alpha:1.000],
                                 } range:NSMakeRange(5, 9)];
        [label setAttributedText:aString];
        [_noNet addSubview:label];
        [_noNet addSubview:button];
        
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(60, 210, 80, 25);
        btn.backgroundColor = [UIColor colorWithWhite:0.200 alpha:1.000];
        btn.layer.cornerRadius = 6;
        btn.layer.masksToBounds = YES;
        [btn setTitle:@"重新加载" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(noResultButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_noNet addSubview:btn];
        
       
        
    }
    
    return _noNet;
}

-(UIView *)noDataView{
    if (!_noDataView) {
        
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake((_backgroundScrollView.frame.size.width-200)/2, (_backgroundScrollView.frame.size.height-200)/2, 200, 235)];
        _noDataView.hidden = TRUE;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame = CGRectMake(45, 45, 110, 110);
        [button setImage:[UIImage imageNamed:@"img_nodata"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(noResultButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 - 44, 200, 44)];
        label.text = @"暂无数据";
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:14.0f];
        label.textColor = [UIColor colorWithWhite:0.000 alpha:0.540];
        [_noDataView addSubview:label];
        [_noDataView addSubview:button];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(60, 210, 80, 25);
        btn.backgroundColor = [UIColor colorWithWhite:0.200 alpha:1.000];
        btn.layer.cornerRadius = 6;
        btn.layer.masksToBounds = YES;
        [btn setTitle:@"点击返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
        [_noDataView addSubview:btn];
        
    }
    
    return _noDataView;
}

- (void)noResultButtonAction:(id)sender
{
    [self httpRequest];
}


-(UIButton *)aplyBtn
{
   
    if (!_aplyBtn)
    {
        _aplyBtn = [[UIButton alloc]init];
        [_aplyBtn setTitle:@"点击报名" forState:UIControlStateNormal];
        [_aplyBtn addTarget:self action:@selector(buttonApplyTheExamTapped:) forControlEvents:UIControlEventTouchUpInside];
        _aplyBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [_aplyBtn setBackgroundColor:kColorOrangeNormal forState:UIControlStateNormal];
        [_aplyBtn setBackgroundColor:kColorOrangePressed forState:UIControlStateHighlighted];
        [_aplyBtn setBackgroundColor:kColorOrangeDisabled forState:UIControlStateDisabled];
        _aplyBtn.layer.cornerRadius = 5;
        _aplyBtn.layer.masksToBounds = YES;
        _aplyBtn.hidden = YES;
       
    }
    
    return _aplyBtn;
    
}
-(void)buttonApplyTheExamTapped:(id)sender
{
    if ([AccountTools isLogin]==NO) {
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        loginVC.backBtHide = FALSE;
        [self presentViewController:loginVC animated:YES completion:^(void){}];
        
    }else{
     
        DetailActivity *activity =  [_dataSourceArr objectAtIndex:0];
        ApplyConfirmationViewController *applyConfirmationVC = [[ApplyConfirmationViewController alloc]init];
        applyConfirmationVC.activityName = activity.title;
        applyConfirmationVC.activityDestion = activity.address;
        applyConfirmationVC.activityTime = activity.activityTime;
        applyConfirmationVC.activityId = activity.activitiesId;
        [self.navigationController pushViewController:applyConfirmationVC animated:YES];
        
        
    }
    
        
  
}

#pragma mark  webDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    float totalHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
    
    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, totalHeight);
    
    
    _aplyBtn.frame = CGRectMake(SCREEN_WIDTH/2-50.0f, totalHeight+16.0f, 100.0f, 44.0f);
    if ([self.valid isEqualToString:@"1"]) {
        
        self.aplyBtn.hidden = NO;
        _backgroundScrollView.contentSize = CGSizeMake(_backgroundScrollView.frame.size.width, webView.frame.origin.y + totalHeight +76.0f);
        
    }else{
        self.aplyBtn.hidden = YES;
        _backgroundScrollView.contentSize = CGSizeMake(_backgroundScrollView.frame.size.width, webView.frame.origin.y + totalHeight );
       
    }


     [[AnimationIndicator sharedIndicator]stopAnimationWithLoadText:@"" withType:YES];
    

    
}


@end
